package com.hcs.chenyu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hcs.chenyu.chenyuusedmarket.usedmarket;
import com.hcs.chenyu.confessionwall.confessionwall;
import com.hcs.chenyu.library.bookadministration;
import com.hcs.chenyu.moreaboutstudent.more;

public class loadingmain extends AppCompatActivity implements View.OnClickListener{
    private TextView topBar;
    private Button tabDeal;
    private Button tabPoi;
    private Button tabMore;
    private Button tabUser;
    private FrameLayout ly_content;

    private FirstFragment f1,f2,f3,f4;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_loadingmain);

        bindView();


    }

    //UI组件初始化与事件绑定
    private void bindView() {
        topBar = (TextView) this.findViewById(R.id.txt_top);
        tabDeal = (Button) this.findViewById(R.id.txt_deal);
        tabPoi = (Button) this.findViewById(R.id.txt_poi);
        tabUser = (Button) this.findViewById(R.id.txt_user);
        tabMore = (Button) this.findViewById(R.id.txt_more);
        ly_content = (FrameLayout) findViewById(R.id.fragment_container);

        tabDeal.setOnClickListener(this);
        tabMore.setOnClickListener(this);
        tabUser.setOnClickListener(this);
        tabPoi.setOnClickListener(this);

    }

    //重置所有文本的选中状态
    public void selected(){
        tabDeal.setSelected(false);
        tabMore.setSelected(false);
        tabPoi.setSelected(false);
        tabUser.setSelected(false);
    }

    //隐藏所有Fragment
    public void hideAllFragment(FragmentTransaction transaction){
        this.transaction = transaction;
        if(f1!=null){
            transaction.hide(f1);
        }
        if(f2!=null){
            transaction.hide(f2);
        }
        if(f3!=null){
            transaction.hide(f3);
        }
        if(f4!=null){
            transaction.hide(f4);
        }
    }

    @Override
    public void onClick(View v) {
        //FragmentTransaction transaction = getFragmentManager().beginTransaction();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch(v.getId()){
            case R.id.txt_deal:
                selected();
                tabDeal.setSelected(true);
                if(f1==null){
                    f1 = new FirstFragment("图书管理系统");
                    transaction.add(R.id.fragment_container,f1);
                    Intent intent = new Intent(loadingmain.this, bookadministration.class);
                    startActivity(intent);
                }else{
                    transaction.show(f1);
                    Intent intent = new Intent(loadingmain.this,bookadministration.class);
                    startActivity(intent);
                }
                break;

            case R.id.txt_poi:
                selected();
                tabPoi.setSelected(true);
                if(f2==null){
                    f2 = new FirstFragment("表白墙");
                    transaction.add(R.id.fragment_container,f2);
                    Intent intent1 = new Intent(loadingmain.this, confessionwall.class);
                    startActivity(intent1);
                }else{
                    transaction.show(f2);
                    Intent intent1 = new Intent(loadingmain.this,confessionwall.class);
                    startActivity(intent1);
                }
                break;

            case R.id.txt_user:
                selected();
                tabUser.setSelected(true);
                if(f3==null){
                    f3 = new FirstFragment("二手市场");
                    transaction.add(R.id.fragment_container,f3);
                    Intent intent2 = new Intent(loadingmain.this, usedmarket.class);
                    startActivity(intent2);
                }else{
                    transaction.show(f3);
                    Intent intent3 = new Intent(loadingmain.this, usedmarket.class);
                    startActivity(intent3);
                }
                break;

            case R.id.txt_more:
                selected();
                tabMore.setSelected(true);
                if(f4==null){
                    f4 = new FirstFragment("更多");
                    transaction.add(R.id.fragment_container,f4);
                    Intent intent4 = new Intent(loadingmain.this, more.class);
                    startActivity(intent4);
                }else{
                    Intent intent4 = new Intent(loadingmain.this,more.class);
                    startActivity(intent4);
                    transaction.show(f4);
                }
                break;

        }

        transaction.commit();
    }
}
